package com.qcc.jvmcore02;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * @创建人 qianchengcheng
 * @创建时间 2022/1/16
 * @描述
 */
public class OkHttpUtil {

    public static OkHttpClient client = new OkHttpClient();

    public static String get(String url){
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            if (response.isSuccessful()){
                return response.body().string();
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (null != response){
                response.close();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String url = "http://localhost:8081";
        String responseStr = get(url);
        System.out.println("responseStr:"+responseStr);
    }
}
